//
//  AppDelegate.swift
//  xmly
//
//  Created by 刘文超 on 2019/12/26.
//  Copyright © 2019 刘文超. All rights reserved.
//

import UIKit
import ESTabBarController_swift
import SwiftMessages
import Alamofire
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
    lazy var reacheBlaity :NetworkReachabilityManager? = {
        return NetworkReachabilityManager.init(host: "http://app.u17.com")
    }()

    var window: UIWindow?
    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      
        let tabbar = self.setUpTabbarStyle(delegate: self as? UITabBarControllerDelegate)
        self.window?.backgroundColor = UIColor.white
        self.window?.makeKeyAndVisible()
        self.window?.bounds = UIScreen.main.bounds
        self.window?.rootViewController = tabbar;
        
        
        reacheBlaity?.listener = { status in
            switch status {
            case .reachable(.wwan):
                let msgView =  MessageView.viewFromNib(layout: .cardView)
                msgView.configureTheme(.warning)
                msgView.configureContent(title: "切换数据环境", body: "4G网络")
                var cf = SwiftMessages.defaultConfig
                cf.presentationStyle = .bottom
                SwiftMessages.show(config:cf, view: msgView)
                break
            case .reachable(.ethernetOrWiFi):
                let msgView =  MessageView.viewFromNib(layout: .cardView)
                               msgView.configureTheme(.warning)
                               msgView.configureContent(title: "切换无线网环境", body: "wifi网络")
                               var cf = SwiftMessages.defaultConfig
                               cf.presentationStyle = .bottom
                               SwiftMessages.show(config:cf, view: msgView)
                break
            case .notReachable:
                  let msgView =  MessageView.viewFromNib(layout: .cardView)
                                             msgView.configureTheme(.warning)
                                             msgView.configureContent(title: "切换无网环境", body: "无网络")
                                             var cf = SwiftMessages.defaultConfig
                                             cf.presentationStyle = .bottom
                                             SwiftMessages.show(config:cf, view: msgView)
                break
            default:
              break
            }
             
        }
        reacheBlaity?.startListening()
        
        return true
    }
    
    func setUpTabbarStyle(delegate: UITabBarControllerDelegate?) -> ESTabBarController {
        let tabBarController = ESTabBarController()
        tabBarController.title = "xmly"
        tabBarController.delegate = delegate
        tabBarController.tabBar.shadowImage = UIImage.init(named: "transparent")
        //设置中间按钮
        tabBarController.shouldHijackHandler = {
            tabBarController , ViewController , index in
            if index == 2 {
                return true
            }
            return false
        }
        tabBarController.didHijackHandler = {
         tabBarController , ViewController , index in
            DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
                let WaringMessage = MessageView.viewFromNib(layout: .cardView)
                WaringMessage.configureTheme(.warning)
                WaringMessage.configureDropShadow()
                
                let iconText = ["🤔", "😳", "🙄", "😶"].LS_random()
                WaringMessage.configureContent(title: "警告⚠️", body: "功能还未完善", iconText: iconText!)
//              WaringMessage.button?.isHidden = true
                var warningConfig = SwiftMessages.defaultConfig
                warningConfig.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
                SwiftMessages.show(config: warningConfig, view: WaringMessage)
            }
        }
        
        let home = HomeViewController()
        let listen = ListenViewController()
        let play = PlayViewController()
        let find = FindViewController()
        let mine = MineViewController()
        
        home.tabBarItem = ESTabBarItem.init(BaseIrregularityContentView(), title: "首页", image: UIImage.init(named: "home"), selectedImage: UIImage.init(named: "home_1"))
        home.title = "首页";
        
        listen.tabBarItem = ESTabBarItem.init(BaseIrregularityContentView(), title: "我听", image: UIImage.init(named: "find"), selectedImage: UIImage.init(named: "find_1"))
        listen.title = "我听"
        
        play.tabBarItem = ESTabBarItem.init(BaseIrregularity(), title: "中心", image: UIImage.init(named: "tab_play"), selectedImage: UIImage.init(named: "tab_play"))
        play.title = "播放"
        
        find.tabBarItem = ESTabBarItem.init(BaseIrregularityContentView(), title: "发现", image: UIImage.init(named: "favor"), selectedImage: UIImage.init(named: "favor_1"))
        find.title = "发现"
        
        mine.tabBarItem = ESTabBarItem.init(BaseIrregularityContentView(), title: "我的", image: UIImage.init(named: "me"), selectedImage: UIImage.init(named: "me_1"))
        mine.title = "我的"
        
        let homeNav = BaseNavViewController.init(rootViewController: home)
        let listenNav = BaseNavViewController.init(rootViewController: listen)
        let playNav = BaseNavViewController.init(rootViewController: play)
        let findNav = BaseNavViewController.init(rootViewController: find)
        let mineNav = BaseNavViewController.init(rootViewController: mine)
        
        tabBarController.viewControllers = [homeNav,listenNav,playNav,findNav,mineNav]
        return tabBarController
        
    }
    
    
    
    

//    // MARK: UISceneSession Lifecycle
//
//    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
//        // Called when a new scene session is being created.
//        // Use this method to select a configuration to create the new scene with.
//        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
//    }
//
//    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
//        // Called when the user discards a scene session.
//        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
//        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
//    }


}

